﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Windows;
using BrainTechLLC;
using ImageAppDataModels;
using ImageAppViewModels;

namespace FindDuplicates.Plugin
{
    public partial class CompareImagesWindow : Window
    {
        public event EventHandler<ObjectEventArgs<FileImageViewModel, FileImageViewModel>> ClosedWindow;

        public static readonly DependencyProperty FileImageViewModelsProperty =
            DependencyProperty.Register("FileImageViewModels", typeof(Pair<FileImageViewModel, FileImageViewModel>), typeof(CompareImagesWindow), new PropertyMetadata(null, OnFileImageViewModelsPropertyChanged));

        public Pair<FileImageViewModel, FileImageViewModel> FileImageViewModels
        {
            get { return (Pair<FileImageViewModel, FileImageViewModel>)GetValue(FileImageViewModelsProperty); }
            set { SetValue(FileImageViewModelsProperty, value); }
        }

        private static void OnFileImageViewModelsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CompareImagesWindow source = d as CompareImagesWindow;
            Pair<FileImageViewModel, FileImageViewModel> v = e.NewValue as Pair<FileImageViewModel, FileImageViewModel>;
            Pair<FileImageViewModel, FileImageViewModel> vOld = e.OldValue as Pair<FileImageViewModel, FileImageViewModel>;

            if (v != vOld)
            {
                source.UpdateFileImageViewModel(v, vOld);
            }
        }

        private void UpdateFileImageViewModel(Pair<FileImageViewModel, FileImageViewModel> viewModels, Pair<FileImageViewModel, FileImageViewModel> old)
        {
            FileImageViewModels = viewModels;
            compare.FileImageViewModel1 = viewModels.obj1;
            compare.FileImageViewModel2 = viewModels.obj2;            
        }

        public CompareImagesWindow()
        {
            InitializeComponent();
            Closing += new System.ComponentModel.CancelEventHandler(CompareImagesWindow_Closing);
        }

        void CompareImagesWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {         
            if (ClosedWindow != null)
                ClosedWindow(this, new ObjectEventArgs<FileImageViewModel, FileImageViewModel>(compare.FileImageViewModel1, compare.FileImageViewModel2));
        }
    }
}
